package com.leeon.generics;

import java.util.Iterator;

public class IterableFibonacci extends Fibonacci implements Iterable<Integer> {

	private int n;
	
	public IterableFibonacci(int count) {
		n = count;
	}
	
	@Override
	public Iterator<Integer> iterator() {
		return new FibonacciIterator();
	}

	class FibonacciIterator implements Iterator<Integer>{
		
		private int count = n;
		
		@Override
		public boolean hasNext() {
			return count > 0;
		}

		@Override
		public Integer next() {
			count--;
			return IterableFibonacci.this.next();
		}

		@Override
		public void remove() {
			throw new RuntimeException();
		}
	}
	
	public static void main(String[] args){
		for (int i : new IterableFibonacci(18)) {
			System.out.print("  " + i);
		}
	}
}
